package com.l;

/**
 * Given a 32-bit signed integer, reverse digits of an integer.
 *
 * Example 1:
 *
 * Input: 123
 * Output: 321
 * Example 2:
 *
 * Input: -123
 * Output: -321
 * Example 3:
 *
 * Input: 120
 * Output: 21
 * Note:
 * Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
 */
public class Reverse_Integer {

    public int reverse(int x) {
        //我自己的跑不成功
//        boolean isGtZore = x >= 0;
//        String s = String.valueOf(Math.abs(x));
//        int length = s.length();
//        char[] chars = s.toCharArray();
//        StringBuffer stringBuffer = new StringBuffer();
//        for (int i = length - 1; i >= 0; i--) {
//            stringBuffer.append(chars[i]);
//        }
//        Integer integer = Integer.valueOf(stringBuffer.toString());
//        return isGtZore ? integer : -1 * integer;
//这是别人的
        long result =0;
        while(x != 0)
        {
            result = (result*10) + (x%10);
            if(result > Integer.MAX_VALUE) return 0;
            if(result < Integer.MIN_VALUE) return 0;
            x = x/10;
        }
        return (int)result;
    }

    public static void main(String[] args) {
        Reverse_Integer integer = new Reverse_Integer();
//        int reverse = integer.reverse(9646324351);//这个是系统给的其中一个测试值
//        System.out.print(reverse);
    }
}
